%%%-------------------------------------------------------------------
%%% @author wukai
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 31. 8月 2020 11:11 上午
%%%-------------------------------------------------------------------
-author("wukai").
-ifndef(HIMQ).
-define(HIMQ, himq).
-define(UINT, 32 / unsigned - big - integer).
-define(INT, 32 / signed - big - integer).
-define(USHORT, 16 / unsigned - big - integer).
-define(SHORT, 16 / signed - big - integer).
-define(UBYTE, 8 / unsigned - big - integer).
-define(BYTE, 8 / signed - big - integer).
-define(SESSION_TAB, himq_session_table).
-define(TOPIC_ONLINE,<<"$sys/client/connect">>).
-define(TOPIC_OFFLINE,<<"$sys/client/disconnect">>).
%%mqtt config key
-define(LISTENER_MQTT_IP, listener_mqtt_ip).
-define(LISTENER_MQTT_PORT, listener_mqtt_port).
-define(LISTENER_MQTT_WEBSOCKET, listener_mqtt_websocket).
-define(LISTENER_MQTT_SSL, listener_mqtt_ssl).
-define(LISTENER_MQTT_SSL_CA_FILE, listener_mqtt_ssl_cafile).
-define(LISTENER_MQTT_SSL_CAC_FILE, listener_mqtt_ssl_cacertfile).
-define(LISTENER_MQTT_SSL_KEY_FILE, listener_mqtt_ssl_keyfile).
-define(LISTENER_MQTT_MAX_ACCEPTORS, listener_max_acceptors).
-define(LISTENER_MQTT_MAX_CONN, listener_max_connections).
-define(LISTENER_MQTT_MAX_PACKET_SIZE, listener_max_packetsize).
-define(LISTENER_HTTP_ADMIN, listener_http_admin).


%% himq_admin config key
-define(HIMQ_LISTENER_HTTP_IP, himq_listener_http_ip).
-define(HIMQ_LISTENER_HTTP_PORT, himq_listener_http_port).
-define(TOKEN_EXPIRE_TIME, 7 * 24 * 60 * 60).
-define(TOKEN_KEY, <<"himq_admin">>).
-define(APP_KEY, <<"himq_app">>).
-define(HIMQ_APP,himq_app).
-define(HIMQ_AUTH_APP,auth_app).



%%%tables Define
-define(TABLE_SEQUENCE,table_sequence).
-define(TABLE_SETTING,himq_setting).
-define(TABLE_COMMAND,himq_command).
-define(TABLE_CONNECTION,himq_connection).
-define(TABLE_APPLICATION,himq_auth_app).
-define(TABLE_ADMIN,himq_admin).
-define(TABLE_MQTT_USER,himq_mqtt_user).
-define(TABLE_DEVICE,himq_device).
-define(TABLE_LINK,himq_link).
-define(TABLE_RETAIN,himq_retain).
-define(TABLE_UNDELIVERED,himq_undelivered).
-define(TABLE_PUSH,himq_push).

%%%setting define

-define(SETTING_BASE_KEY,setting_base_key).
-define(SETTING_PUSH_KEY,setting_push_key).

-record(?TABLE_SEQUENCE, {name, sequence}).%自增索引表，维护其他表的自增id
-record(himq_setting,{key,value}).

-record(himq_conn, {client_id,ip,type=tcp, remote_port, peer_name, buffer = <<>>, p_state = #{}, protocol, socket, trans, receive_timeout = 60000}).
-record(himq_session, {client, type, mod, remark,socket, add_time,protocol,pv, rec_pid, send_pid, status}).
%%指令表集合
-record(himq_command,{id,name,type,topic,packet,created_at}).
-record(himq_push,{id,client,name,packet,topic,qos,retain,created_at,status,end_time,app}).
-record(himq_auth_app,{key,name,creator,enable,remark,create_at,updated_at}).
-record(himq_admin,{username,password,token,expire}).
-record(himq_mqtt_user,{username,password,creator,remark,created_at,updated_at,enable=true}).
-record(himq_device,{id,creator,remark,user,created_at,updated_at,enable=true}).

-record(himq_link,{id,status,protocol,remark,created_at}).
%%create table if not exists himq_retain
%%(
%%    topic      varchar(255) primary key comment '主题',
%%    qos        integer comment '消息质量',
%%    packet     text comment '消息内容',
%%    id     varchar(255) comment '所属设备',
%%    created_at timestamp not null default current_timestamp comment '创建时间',
%%    updated_at timestamp          default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间'
%%);

-record(himq_retain,{topic,id,qos,packet,created_at,updated_at}).
%%##未发送的消息 离线消息
%%create table if not exists himq_undelivered
%%(
%%    id     varchar(255) comment '所属设备',
%%    uuid     varchar(255) comment '所属设备',
%%    retain     integer,
%%    dup        integer            default 1 comment '是否重复消息',
%%    topic      text comment '主题',
%%    qos        integer comment '消息质量',
%%    packet     text comment '消息内容',
%%    source     varchar(255) comment '消息来源',
%%    remark      varchar(255) comment '备注',
%%    step       varchar(255) comment '发送阶段',
%%    created_at timestamp not null default current_timestamp comment '创建时间',
%%    updated_at timestamp          default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间'
%%);

-record(himq_undelivered,{uuid,id,retain,dup,topic,qos,packet,from,step,remark,created_at}).
-endif.